package com.whfc.fvs.service.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.common.exception.BizException;
import com.whfc.fvs.dao.FvsSettingMapper;
import com.whfc.fvs.dto.FvsSettingDTO;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.redis.EzvizApiTokenRedisDao;
import com.whfc.fvs.redis.ImouApiTokenRedisDao;
import com.whfc.fvs.service.FvsPlatformService;
import com.whfc.fvs.service.factroy.FvsPlatformServiceFactory;
import com.whfc.fvs.service.impl.FvsEzvizVsServiceImpl;
import com.whfc.fvs.service.impl.FvsImouVsServiceImpl;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-09-24 20:04
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class FvsAccessTokenJob {

    @Autowired
    private FvsPlatformServiceFactory platformServiceFactory;

    @Autowired
    private FvsSettingMapper fvsSettingMapper;

    @Autowired
    private EzvizApiTokenRedisDao ezvizApiTokenRedisDao;

    @Autowired
    private ImouApiTokenRedisDao imouApiTokenRedisDao;

    /**
     * 海康AccessToken可以保存7天，所以此处定时任务设置为 每周日凌晨1点执行一次,保存到 redis中
     */
    @XxlJob("refreshEzvizAccessToken")
    public void refreshEzvizAccessToken() {
        try {
            XxlJobHelper.log("开始获取萤石AccessToken...");
            initAccessToken(VsPlatform.HIK_EZVIZ);
        } catch (Exception e) {
            XxlJobHelper.handleFail("萤石云获取AccessToken出错,error:" + e.getMessage());
        }
    }


    /**
     * 大华AccessToken可以保存3天，所以此处定时任务设置为 每三天执行一次,保存到 redis中
     */
    @XxlJob("refreshImouAccessToken")
    public void refreshImouAccessToken() {
        try {
            XxlJobHelper.log("开始获取大华AccessToken...");
            initAccessToken(VsPlatform.DAHUA_IMOU);
        } catch (Exception e) {
            XxlJobHelper.handleFail("大华乐橙获取AccessToken出错,错误:{}" + e.getMessage());
        }
    }


    private void initAccessToken(VsPlatform platform) throws BizException {
        XxlJobHelper.log("初始化监控云服务token, platform:{}", platform);
        List<FvsSettingDTO> fvsSettingList = fvsSettingMapper.selectAppKeySecretList(platform.getValue());
        FvsPlatformService platFromService = platformServiceFactory.getFvsPlatFromService(platform);
        for (FvsSettingDTO fvsSettingDTO : fvsSettingList) {
            if (VsPlatform.HIK_EZVIZ.equals(platform)) {
                FvsEzvizVsServiceImpl fvsEzvizVsService = (FvsEzvizVsServiceImpl) platFromService;
                String accessToken = fvsEzvizVsService.initAccessToken(fvsSettingDTO.getAppKey(), fvsSettingDTO.getAppSecret());
                ezvizApiTokenRedisDao.setAccessToken(fvsSettingDTO.getAppKey(), accessToken);
            } else if (VsPlatform.DAHUA_IMOU.equals(platform)) {
                FvsImouVsServiceImpl fvsImouVsService = (FvsImouVsServiceImpl) platFromService;
                String accessToken = fvsImouVsService.initAccessToken(fvsSettingDTO.getAppKey(), fvsSettingDTO.getAppSecret());
                imouApiTokenRedisDao.setAccessToken(fvsSettingDTO.getAppKey(), accessToken);
            }
        }
    }
}
